<?php

require '../src/facebook.php';

$facebook = new Facebook(array(
  'appId'  => '370408382994780',
  'secret' => 'd98482bf563065c3cf0ccc556c649224',
));

// See if there is a user from a cookie
$user = $facebook->getUser();

if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me');
    $perms = $facebook->api('/me/permissions');
  } catch (FacebookApiException $e) {
    echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
    $user = null;
  }
}

?>
<!DOCTYPE html><!--schema.org -->
    <html xmlns:fb="http://ogp.me/ns/fb#" prefix="og: http://ogp.me/ns#">
        <head>
            <title>RAGE'n'FURIOUS</title>
            <meta property="og:title" content="RAGE'n'FURIOUS ~ the.game" />
            <meta property="og:type" content="website" />
            <meta property="og:url" content="http://ragenfurious.fr.nf/" />
            <meta property="og:image" content="https://legb6q.sn2.livefilestore.com/y1p1uQdoNRvW5Ooxxgr5x3gGYyaMdcxRWYZGRHZ3BB7Mdgl6ZDCglcbSi1UO8TTgwLJYUbTWVUH-0fuHhVnACmz7UMkEvncCU5C/nyan.png?psid=1" />
            <meta property="fb:app_id" content="370408382994780" />
            <meta property="fb:admins" content="1159350514" />
            <meta property="og:locale" content="fr_FR" />
            <meta property="og:locale:alternate" content="en_GB" />
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        </head>
        <body>
            <div id="fb-root"></div>
            <script>(function(d){
                    var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
                    if (d.getElementById(id)) {return;}
                    js = d.createElement('script'); js.id = id; js.async = true;
                    js.src = "//connect.facebook.net/fr_FR/all.js#xfbml=1&appId=370408382994780";
                    ref.parentNode.insertBefore(js, ref);
                }(document));
                // Init the SDK upon load
                window.fbAsyncInit = function() {
                    FB.init({
                    appId      : '370408382994780', // App ID
                    channelUrl : '//'+window.location.hostname+'/channel', // Path to your Channel File
                    status     : true, // check login status
                    cookie     : true, // enable cookies to allow the server to access the session
                    xfbml      : true  // parse XFBML
                    });

                    // listen for and handle auth.statusChange events
                    FB.Event.subscribe('auth.statusChange', function(response) {
                    if (response.authResponse) {
                        // user has auth'd your app and is logged into Facebook
                        FB.api('/me', function(me){
                        if (me.name) {
                            document.getElementById('auth-displayname').innerHTML = me.name;
                        }
                        })
                        document.getElementById('auth-loggedout').style.display = 'none';
                        document.getElementById('auth-loggedin').style.display = 'block';
                    } else {
                        // user has not auth'd your app, or is not logged into Facebook
                        document.getElementById('auth-loggedout').style.display = 'block';
                        document.getElementById('auth-loggedin').style.display = 'none';
                    }
                    });
                    
                    FB.Event.subscribe('auth.login', function(response) {
                    window.location.reload();
                    });
                    FB.Event.subscribe('auth.logout', function(response) {
                    window.location.reload();
                    });

                    // respond to clicks on the login and logout links
                    document.getElementById('auth-loginlink').addEventListener('click', function(){
                    FB.login();
                    });
                    document.getElementById('auth-logoutlink').addEventListener('click', function(){
                    FB.logout();
                      
                    }); 
                } 
            </script>
            <h1>Facebook Client-side Authentication Example</h1>
      <div id="auth-status">
        <div id="auth-loggedout">
          <a href="#" id="auth-loginlink">Login</a>
        </div>
        <div id="auth-loggedin" style="display:none">
          Hi, <span id="auth-displayname"></span>  
        (<a href="#" id="auth-logoutlink">logout</a>)
      </div>
    </div>

    <?php if ($user) { ?>
      <script>function post() { FB.ui(
  {
   method: 'feed',
   message: 'dsqdsdsqlkfdsqjdsqk',
   name: 'Connect',
   caption: 'The Facebook Connect JavaScript SDK',
      description: (
      'A small JavaScript library that allows you to harness ' +
      'the power of Facebook, bringing the user\'s identity, ' +
      'social graph and distribution power to your site.'
   ),
   link: 'http://ragenfurious.fr.nf/',
   
   actions: [
        { name: 'fbrell', link: 'http://www.fbrell.com/' }
   ],
  user_message_prompt: 'Share your thoughts about RELL'
  },
  function(response) {
    if (response && response.post_id) {
      alert('Post was published.');
    } else {
      alert('Post was not published.');
    }
  }
);}</script>
            <script>FB.api(
                      {
                        method: 'fql.query',
                        query: 'select name,pic_small,online_presence,uid from user where uid in (select uid2 FROM friend WHERE uid1 = me()) and online_presence != "offline"'
                      },
                      function(response) {
                    for(i in response){
                        document.getElementById("presence").innerHTML += "<img src='"+response[i].pic_small+"' alt='' /> "+response[i].name + " ("+response[i].uid+") - "+ response[i].online_presence+"<br />"; }
                      }
                    );</script>
      <div onclick="post()" >Click salope</div><br />
        <!--   <fb:login-button
registration-url="https://developers.facebook.com/docs/plugins/registration" /> https://developers.facebook.com/docs/plugins/registration/ -->
        
        <br /><br />
      Your user profile is <fb:send  font="segoe ui"></fb:send><br />
      <fb:facepile href="http://ragenfurious.fr.nf/" max_rows="1" width="500"></fb:facepile><br />
      <fb:like send="true" width="450" show_faces="true"></fb:like>
      <pre>
        <?php print htmlspecialchars(print_r($user_profile, true)) ?>
      </pre>
            <br /><div id="presence"></div><br />
            <pre>
        <?php print htmlspecialchars(print_r($perms, true)) ?>
<?php $default_perms = array("email","user_about_me", "user_birthday","friends_online_presence","publish_stream");
 $stop_fb = false;
 foreach($default_perms as $p)
 { if(!array_key_exists($p, $perms["data"][0])) { $stop_fb = true; }
 
 } if($stop_fb){ ?>
<fb:login-button scope="email,user_about_me, user_birthday,friends_online_presence,publish_stream"></fb:login-button><?php } ?>
      </pre>
    <?php } else { ?>
      <fb:login-button scope="email,user_about_me, user_birthday,friends_online_presence,publish_stream"></fb:login-button>
      <fb:registration 
  fields="[
 {'name':'name'},
 {'name':'pseudo',      'description':'Pseudonyme',             'type':'text'},
 {'name':'email'},
 {'name':'password'},
 {'name':'gender'},
 {'name':'birthday'}
]" 
  redirect-uri="http://ragenfurious.fr.nf/lobby/register_finish/"
  width="530">
</fb:registration>

    <?php } ?>

  </body>
</html>
